package com.hainatong.shiphelper.module.workermgr;


import android.content.Context;
import android.content.Intent;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.ActivityWorkerMgrBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.warehouse.entity.OrgUser;
import com.hainatong.shiphelper.module.warehouse.entity.ShipOrgBean;
import com.hainatong.shiphelper.module.workermgr.adapter.OrgExpandableAdapter;
import com.hainatong.shiphelper.module.workermgr.viewmodel.OrgUserViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;

import java.util.List;

public class WorkerMgrActivity extends BaseMvvmActivity<ActivityWorkerMgrBinding, OrgUserViewModel>
{
    //private List<Fragment> fragmentList;
    //private MyFragmentPagerAdapter vpAdapter;
    private List<ShipOrgBean> shipOrgList;
    private OrgExpandableAdapter orgAdapter;

    public static void start(Context context)
    {
        context.startActivity(new Intent(context, WorkerMgrActivity.class));
        //((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_worker_mgr;
    }

    @Override
    protected int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initView()
    {
        setToolbarTitle(viewDataBinding.includeTitle.toolbar,getString( R.string.worker_mgr_str )  );
        setToolBar( viewDataBinding.includeTitle.toolbar );
        /*
        fragmentList = new ArrayList<>();
        fragmentList.add( new DepartmentFragment());
        fragmentList.add( new WorkerFragment());
        vpAdapter = new MyFragmentPagerAdapter( this,fragmentList );
        viewDataBinding.vp2.setAdapter( vpAdapter );
        viewDataBinding.vp2.setOffscreenPageLimit( 2 );
        */
        orgAdapter = new OrgExpandableAdapter( this );
        viewDataBinding.expandListview.setAdapter( orgAdapter );
        viewDataBinding.expandListview.setOnChildClickListener( (parent, v, groupPosition, childPosition, id) -> {
            OrgUser mOrg = ((OrgUser)orgAdapter.getChild(groupPosition, childPosition));
            // 跳转 根据 roleID 选择人员
            ComFragmentActivity.startViewByParam(this,Constants.VIEW_WORKER_SELECT_VIEW, GsonUtil.obj2json(mOrg),true);

            return false;
        });

    }

    @Override
    protected void initData()
    {
        // 先 查询 人员 组织  (中文查询)
        viewModel.queryOrgUserListByLan("zh");
        viewModel.getOrgUserResult().observe( this ,result -> {
            if( result.isSuccess() )
            {
                shipOrgList = result.getData();
                orgAdapter.setShipOrgBeans( shipOrgList );
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    private void setToolbarTitle(Toolbar toolbar, String title)
    {
        // toolbar.setBackgroundColor(ContextCompat.getColor(this,R.color.red));

        ((TextView)toolbar.findViewById(R.id.tv_title)).setText(title);
        ((ImageButton)toolbar.findViewById(R.id.btn_back)).setImageResource(R.drawable.svg_back);
        toolbar.findViewById(R.id.btn_back).setOnClickListener(v -> {
            supportFinishAfterTransition();
        });
    }



}
